using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
// 未完成的摇杆
public class JoyStick : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
    // 移动半径
    public float radius;
    // 摇杆中心位置
    private Vector3 orginPos;
    // 移动距离
    private float distance;
    // 鼠标位置的方向向量
    private Vector3 direction;

    public void OnBeginDrag(PointerEventData eventData)
    {

    }

    public void OnDrag(PointerEventData eventData)
    {
        distance = Vector3.Distance(orginPos, transform.position);
        if (distance < radius)
        {
            transform.position = Input.mousePosition;
        }
        else
        {
            // 让鼠标移动超过摇杆活动半径时，摇杆顶在边缘位置
            direction = Input.mousePosition - orginPos;
            transform.position = direction.normalized * radius + orginPos;
        }
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        transform.position = orginPos;
    }

    // Start is called before the first frame update
    void Start()
    {
        orginPos = transform.position;

    }

    // Update is called once per frame
    void Update()
    {

    }
}
